<?php

class Colegio extends CI_Model {
	
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function get_autosugcodigo()
    {
    	$this->db->select('codigo, nombre');
		$this->db->like('nombre',$this->input->post('queryString'));
		return $this->db->get('colegio', 10);
    }
    
    function add_colegio($data)
    {
    	//print_r($data);
		$dbRet = $this->db->insert('colegio', $data); 
	    if (!$dbRet) {
	      $ret['ErrorMessage'] = $this->db->_error_message();
	      $ret['ErrorNumber'] = $this->db->_error_number();
	      log_message('error', "DB Error: (".$ret['ErrorNumber'].") ".$ret['ErrorMessage']);
	    }
    }
    
    function existe_codigo($codigo)
    {
    	
		log_message('debug','::existe codigo: antes select');
		$this->db->select('codigo');
		$this->db->where('codigo', $codigo); 
		$query = $this->db->get('colegio');
		$nrows=0;
	    foreach ($query->result() as $row)
		{
		    log_message('debug','::existe codigo: '.$row->codigo);
		    $nrows++;
		}		
		log_message('debug','::existe codigo:'.$nrows);
		if($nrows>0){
			return 1;
		} 
		else 
		{
			return 0;
		} 	
    }
    
    
    function getRankingColegios($colegio=null,$start=0,$offset=10){
    	
    	if($start=="") $start=0;
    	//$sql = "SELECT c.*, @i:=@i+1 AS rank FROM colegio c, (select @i:=0) foo ORDER BY puntaje_acumulado desc";
		//$sql2 = "SELECT * from (".$sql.") fuu LIMIT ".$start.", ".$offset;
    	
		$this->db->limit($offset,$start);
		$this->db->order_by('ranking','asc');
		$query = $this->db->get('colegio');
    	
    	return $query->result();
    	
    }
    
    function getCountColegios(){
    	return $this->db->count_all_results('colegio');
    }
    
    
    function getColegio($codigo){
    	
    	$this->db->where('codigo',$codigo);
    	$query = $this->db->get('colegio');
    	$result = $query->result();
    	return $result[0];
    	
    }
    
    function getDistinctActividadesRealizadasColegio($codigo){
    	
    	$this->db->distinct();
    	$this->db->select('tipo');
    	$this->db->where('codigo',$codigo);
    	$this->db->where('estado','A');
    	$query = $this->db->get('header_formulario');
    	
    	return $query->result();
    	
    }
    
    function getTotalRecaudadoColegio($codigo){

    	$sql="select IFNULL(SUM(monto),0) as monto from deposito d join header_formulario h on d.id_header_formulario = h.id_header_formulario where h.estado='A' and h.codigo='".$codigo."'";
		//echo $sql;
    	$query = $this->db->query($sql);
    	$result = $query->result();
    	
    	return $result[0];
    	
    }
    
}

/* End of file colegio.php */
/* Location: ./application/models/forms/colegio.php */